package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.Student;
import com.wanmait.schedule.mapper.StudentMapper;
import com.wanmait.schedule.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 学生表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Override
    public Boolean identityRepeat(String identity) {

        LambdaQueryChainWrapper<Student> chainWrapper = new LambdaQueryChainWrapper<>(studentMapper);
        Integer count = chainWrapper.eq(Student::getIdentity, identity)
                .count();
        return count==0;
    }



//带分页 学生列表 包含使用类型和姓名的模糊查询
    @Override
    public PageInfo<Student> list(Student student, Integer pageNum) {
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,5);
//        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotBlank(student.getName()),Student::getName,student.getName());
//        queryWrapper.eq(student.getSex()!=null,Student::getSex,student.getSex());
//        queryWrapper.eq(student.getClassesId()!=null,Student::getClassesId,student.getClassesId());
//        List<Student> studentList = studentMapper.selectList(queryWrapper);
        if(student.getSex().equals("全部")){
            student.setSex("");
        }
        List<Student> studentList = studentMapper.findConditions(student);
        PageInfo<Student> pageInfo =new PageInfo<>(studentList,11);
        return pageInfo;
    }
    //添加
    @Override
    public Boolean add(Student student) {
        student.setUsername(student.getPhone());
        student.setPassword(student.getIdentity().substring(student.getIdentity().length()-6));
        int insert = studentMapper.insert(student);
        return insert>0;
    }
    //登录
    @Override
    public Student login(String username, String password) {
        LambdaQueryChainWrapper<Student> chainWrapper = new LambdaQueryChainWrapper<>(studentMapper);
        Student loginStudent = chainWrapper
                .eq(Student::getUsername,username)
                .eq(Student::getPassword,password)
                .one();
        /*//更新最后登录时间 登录IP 登录次数
        Admin updateAdmin = new Admin();
        updateAdmin.setId(loginAdmin.getId());
        updateAdmin.setLoginIp(admin.getLoginIp());
        updateAdmin.setLoginTime(LocalDateTime.now());
        updateAdmin.setLoginCount(loginAdmin.getLoginCount()+1);
        //添加登录日志
        return updateAdmin;*/
        return loginStudent;
    }

    /**
     * 根据班级id查询
     * @param id 班级id
     * @return
     */
    @Override
    public List<Student> findByClassStudent(Integer id) {
        LambdaQueryChainWrapper<Student> chainWrapper = new LambdaQueryChainWrapper(studentMapper);
        List<Student> studentList = chainWrapper.eq(Student::getClassesId, id)
                .eq(Student::getState, 0)
                .list();
        return studentList;
    }

    @Override
    public List<Integer> findByAll() {
        List<Integer> byAll = studentMapper.findByAll();

        return byAll;
    }

    @Override
    public List<Student> findByClassId(Integer[] ids) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Student::getClassesId,ids);
        List<Student> studentList = studentMapper.selectList(queryWrapper);
        return studentList;
    }

    @Override
    public Boolean ExcelAdd(List<Student> studentList) {

        studentList.stream().forEach(student -> {
            student.setUsername(student.getIdentity());
            student.setPassword(student.getPhone());
        });

        studentMapper.ExcelAdd(studentList);

        return true;
    }

    @Override
    public Student studentCenter(Integer id) {
        return studentMapper.studentCenter(id);
    }
}
